// hyb_yl/krecovery/pages/postoperative_ordercont/postoperative_ordercont.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    back_orser:"",
    doc:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      back_orser:options.back_orser
    })
    app.util.request({
      'url': 'entry/wxapp/index.base',
      success: function (res) {
        var se = res.data.ztcolor
        wx.setNavigationBarColor({
          frontColor: '#ffffff',
          backgroundColor: se,
        })
      },
      fail: function (err) {
        console.log(err)
      },
    })
    var key_words = options.key_words;
    that.setData({
      key_words:key_words
    })
    that.getOrder_detail();
    // if(key_words == 'yuyuejiuzhen' || key_words == 'zhuyuananpai' || key_words == 'baogaojiaji' || key_words == 'shuhouanpai')
    // {
    //   that.getOrders();
    // }else{
    //   that.getOrder();
    // }
    if(options.doc != '' && options.doc != undefined)
    {
      that.setData({
        doc:options.doc
      })
    }
    
  },

  // 获取术后康复服务包详情
  getOrder_detail:function(){
    var that = this;
    app.util.request({
      'url':"entry/wxapp/follow.fuwuorder_detail",
      data:{
        back_orser:that.data.back_orser,
      },
      success:function(res)
      {
        
        that.setData({
          info:res.data
        })
        
      }
    })
  },
  jiezhen:function(){
    var that = this;
    
    var key_words = that.data.key_words;
    if(key_words == 'shuhoukangfu')
    {
      app.util.request({
        'url':"entry/wxapp/green.shuhou_jiedan",
        data:{
          back_orser:that.data.info.back_orser,
        },
        success:function(res){
          
          app.util.request({
            'url':"entry/wxapp/zhuanjia.shjzmsgdh",
            data:{
              id:that.data.info.id
            },
            success:function(res){
              
            }
          })
          that.getOrder();
        }
      })
    }else{
      app.util.request({
        'url':"entry/wxapp/green.order_accept",
        data:{
          back_orser:that.data.info.back_orser,
          id:that.data.info.id,
          did:that.data.info.did,
          zid:that.data.info.zid,
        },
        success:function(res){
          
          app.util.request({
            'url':"entry/wxapp/zhuanjia.shjzmsgdh",
            data:{
              id:that.data.info.id
            },
            success:function(res){
              
            }
          })
          that.getOrder();
        }
      })
    }
    
  },
  chakwliu: function (e) {
    var that = this
    var orderunique = that.data.info.orderunique
    var id = that.data.info.id
    var orderss = that.data.info.orderss
    var ifpay = that.data.info.ifpay
    //查询默认物流公司
    app.util.request({
      'url': 'entry/wxapp/index.wuliucheack',
      data: {},
      success: function (res) {
        console.log(res)
        var name = res.data.name
        var com = res.data.com
        wx.navigateTo({
          url: '/hyb_yl/userCommunicate/pages/load/load?gsname=' + name + '&orderss=' + orderss + '&id=' + id + '&com=' + com + '&num=' + orderunique + '&ifpay=' + ifpay,
        })
      }
    })
  },
  receive:function(e){
    var that = this;
    var id = that.data.info.id;
    app.util.request({
      'url':"entry/wxapp/green.receive",
      data:{
        id:id
      },
      success:function(res)
      {
        
        that.getOrder();
      }
    })
  },
  getOrder:function(){
    var that = this;
    app.util.request({
      'url':"entry/wxapp/green.shuhou_detail",
      data:{
        back_orser:that.data.back_orser,
      },
      success:function(res)
      {
        that.setData({
          info:res.data
        })
      }
    })
  },
  getOrders:function(){
    var that = this;
    app.util.request({
      'url':"entry/wxapp/green.order_detail",
      data:{
        back_orser:that.data.back_orser,
      },
      success:function(res)
      {
        that.setData({
          info:res.data
        })
      }
    })
  },
  bindphone:function(e){
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.tel,
    })
  },
  // 取消订单
  quxiao:function(){
    var that = this;
    var key_words = that.data.key_words;
    if(key_words == 'shuhoukangfu')
    {
      app.util.request({
        'url':"entry/wxapp/green.shuhou_quxiao",
        data:{
          back_orser:that.data.back_orser
        },
        success:function(res){
          that.getOrder();
        }
      })
    }else{
      app.util.request({
        'url':"entry/wxapp/green.order_change",
        data:{
          id:that.data.info.id,
          ifpay:6
        },
        success:function(res){
          that.getOrders();
        }
      })
    }
    
  },
  // 支付订单
  topay:function(e){
    var that = this
    var orders = that.data.info.order;
    var money = that.data.info.money;
    var key_words = that.data.info.key_words;
    var id = that.data.info.id;
    var zid = that.data.info.zid;
    
    app.util.request({
      'url': 'entry/wxapp/yuyue.paywenzhen',
      header: {
        "Content-Type": "application/xml"
      },
      method: "GET",
      data: {
        openid: wx.getStorageSync('openid'),
        z_tw_money: money,
        orders: orders,
        key_words: key_words
      },
      success: function (res) {
        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr, //字符串随机数
          'package': res.data.package,
          'signType': res.data.signType,
          'paySign': res.data.paySign,
          success: function (res) {
            console.log(res)

          },
          fail: function (res) {
            console.log(res)

          },
          'complete': function (res) {
            //更新问诊时间
            if (res.errMsg == 'requestPayment:ok') {
              if(key_words == 'shuhoukangfu')
              {
                app.util.request({
                  'url': 'entry/wxapp/green.pay_orders',
                  data: {
                    orders: orders,
                  },
                  success: function (res) {
                    console.log(res)
                    that.getOrder();
                  }
                })
              }else{
                app.util.request({
                  'url': 'entry/wxapp/green.pay_order',
                  data: {
                    id: id,
                  },
                  success: function (res) {
                    console.log(res)
                    that.getOrders();
                  }
                })
              }
              
              
              //模板消息提醒
              app.util.request({
                'url': 'entry/wxapp/zhuanjia.getdocmbtxing',
                data: {
                  id: id,
                  near: '术后康复',
                  zid: zid
                },
                success: function (res) {
                  console.log(res)
                  that.setData({
                    page:0,
                    list:[],
                  })
                  
                }
              })
            }
          }
        })
      }
    })
  },


  // 服务包订单接诊
  jiezhens:function(){
    var that = this;
    
    var key_words = that.data.key_words;
    
      app.util.request({
        'url':"entry/wxapp/follow.jiezhen",
        data:{
          back_orser:that.data.info.back_orser,
        },
        success:function(res){
          
          app.util.request({
            'url':"entry/wxapp/zhuanjia.shjzmsgdh",
            data:{
              id:that.data.info.id
            },
            success:function(res){
              
            }
          })
          that.getOrder_detail();
        }
      })
    
    
  },
  // 服务包订单查看物流
  chakwlius: function (e) {
    var that = this
    var orderunique = that.data.info.express
    var orderss = that.data.info.ordersn
    //查询默认物流公司
    app.util.request({
      'url': 'entry/wxapp/index.wuliucheack',
      data: {},
      success: function (res) {
        console.log(res)
        var name = res.data.name
        var com = res.data.com
        wx.navigateTo({
          url: '/hyb_yl/userCommunicate/pages/load/load?gsname=' + name + '&orderss=' + orderss + '&id=' + id + '&com=' + com + '&num=' + orderunique + '&ifpay=' + ifpay,
        })
      }
    })
  },
  receives:function(e){
    var that = this;
    var back_orser = that.data.info.back_orser;
    app.util.request({
      'url':"entry/wxapp/follow.receive",
      data:{
        back_orser:back_orser
      },
      success:function(res)
      {
        
        that.getOrder_detail();
      }
    })
  },

  quxiaos:function(){
    var that = this;
    var key_words = that.data.key_words;
    app.util.request({
      'url':"entry/wxapp/follow.qx_fuwu",
      data:{
        back_orser:that.data.info.back_orser,
      },
      success:function(res){
        that.getOrder_detail();
      }
    })
    
  },
  // 支付订单
  topays:function(e){
    var that = this
    var orders = that.data.info.order;
    var money = that.data.info.total;
    
    app.util.request({
      'url': 'entry/wxapp/follow.pay_fuwu',
      header: {
        "Content-Type": "application/xml"
      },
      method: "GET",
      data: {
        openid: wx.getStorageSync('openid'),
        money: money,
        orders: orders,
      },
      success: function (res) {
        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr, //字符串随机数
          'package': res.data.package,
          'signType': res.data.signType,
          'paySign': res.data.paySign,
          success: function (res) {
            console.log(res)

          },
          fail: function (res) {
            console.log(res)

          },
          'complete': function (res) {
            //更新问诊时间
            if (res.errMsg == 'requestPayment:ok') {
              app.util.request({
                'url': 'entry/wxapp/follow.update_fuwu',
                data: {
                  orders: orders,
                },
                success: function (res) {
                  console.log(res)
                  that.getOrder_detail();
                }
              })
              //模板消息提醒
              app.util.request({
                'url': 'entry/wxapp/zhuanjia.getdocmbtxing',
                data: {
                  id: id,
                  near: '术后康复',
                  zid: zid
                },
                success: function (res) {
                  
                  
                }
              })
            }
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})